package com.web.dao.impl;

import com.entity.Dietitian;
import com.entity.DietitianExample;
import com.mapper.DietitianMapper;
import com.page.PageInfo;
import com.web.dao.DietitianDao;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository("dietitianDao")
public class DietitianDaoImpl implements DietitianDao {
    @Resource
    DietitianMapper dietitianMapper;



    public  Dietitian selectByPrimaryKey(String cid) throws Exception {
        return dietitianMapper.selectByPrimaryKey(cid);
    }



    /**
     *
     * @param pageInfo
     * @return
     */
  public    List<Dietitian> getDietitiansByPage(PageInfo pageInfo) throws Exception {
      Map<String,Object> map = new HashMap<String,Object>();
      map.put("currPage",pageInfo.getCurrPage());
      map.put("pageSize",pageInfo.getPageSize());

      int itemCount = (int)dietitianMapper.countByExample(null);
      pageInfo.setItemCount(itemCount);
      return dietitianMapper.getDietitiansByPage(map);
  }



    public List<String> searchDietitianIdsByName(String petternName){
      /*示例example的用法*/
         DietitianExample dietitianExample = new DietitianExample();
         //查询姓王的会员
        //有两个集合  一个是 List<Criteria> oredCriteria; 控制条件与条件的关系     另外一个是 Criteria 保存的 List<Criterion> criteria;
//ce查询 理解1 createCriteria创建一个条件并加入到条件集合（如果是第一个条件 就加入条件集合）
                dietitianExample.createCriteria().andDnameLike("%"+petternName+"%"
        );
        dietitianExample.setDistinct(true);

//        Function<Dietitian,String> fn = new Function<Dietitian,String>() {
//            @Override
//            public String apply(Dietitian c) {
//                return c.getCid();
//            }
//        };
        return dietitianMapper.selectByExample(dietitianExample).stream().map(dietitian -> {return dietitian.getDid();}).collect(Collectors.toList());
    }


    public int add(Dietitian dietitian) throws Exception {
        return dietitianMapper.insertSelective(dietitian);
    }

    public int update(Dietitian dietitian) throws Exception {
        return dietitianMapper.updateByPrimaryKeySelective(dietitian);
    }
}
